# -*- coding: utf-8 -*-
"""
Created---------------------------------------------------------------------------------------------


Importing things:-----------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
"""
import sys , os , MySQLdb
from PIL import Image
import qrcode


try:
	from karekodyeniyeni import Ui_Form
except:
	print("QRCode is not installed properly.")
	raise SystemExit
try:
	from PyQt4 import QtGui
	from PyQt4 import QtCore
except:
	print("pyqt4-dev-tools'? To install\nsudo apt-get install pyqt4-dev-tools")
	raise SystemExit

		
class MyForm(QtGui.QWidget):
  def __init__(self):
    super(MyForm, self).__init__()
    self.ui = Ui_Form()
    self.ui.setupUi(self)
			
	
    #self.ui.pushButton_2.clicked.connect(self.dbvericek)
    self.ui.pushButton_2.clicked.connect(self.dbkayit)																
															
  


#karekodolusturucu
  def kare_olustur(self, name, num, loc, arc):
	  degisken = name + "-" +str(num) + "-" + loc + "-"  + arc
	  img = qrcode.make(str(degisken))
	  img.save(str(degisken) + '.png')
	  self.ui.label_7.setPixmap(QtGui.QPixmap(degisken + ".png"))
	  self.dbvericek()
  
#degiskenler db-kayıt ve kare olustur fonk baslantici
  def dbkayit(self):
	  name = self.ui.lineEdit.text()
	  num = self.ui.spinBox.value()
	  loc = self.ui.lineEdit_3.text()
	  arc = self.ui.lineEdit_4.text()
	  db = MySQLdb.connect(host="localhost",user="root",passwd="01400140",db="atasam")
	  cursor = db.cursor()
	  cursor.execute("INSERT INTO demirbas2(dbname , dbnum , dbloc , dbarc) VALUES('%s', %s, '%s', '%s');" % (name , num , loc , arc))
	  db.commit()
	  db.close()
	  self.kare_olustur(name, num, loc, arc)
  
#veritabanından veri çek ve sqldeki satır sayısını bul tabloolustur fonk tetikle 
  def dbvericek(self):
	  dbcek = MySQLdb.connect(host="localhost",user="root",passwd="01400140",db="atasam")
	  cursor = dbcek.cursor()
	  cursor.execute("SELECT * FROM demirbas2")
	  row = cursor.fetchall()
	  sqllsatirsayisi = cursor.rowcount
	  for i in row:
		  dbgname = i[0]
		  dbgnum = i[1]
		  dbgloc = i[2]
		  dbgarc = i[3]
		  self.tabloolustur(sqllsatirsayisi, dbgname, dbgnum, dbgloc, dbgarc)


  



#boştablo olustur veritabanından çektiklerini boş listeye bas 
  def tabloolustur(self, sqllsatirsayisi, dbgname, dbgnum, dbgloc, dbgarc):
	  self.table = QtGui.QTableWidget()   
	  self.ui.tableWidget.setRowCount(sqllsatirsayisi)
	  self.ui.tableWidget.setColumnCount(4)
	  self.ui.tableWidget.setItem(1, 1, QtGui.QTableWidgetItem("sdasdasdas"))	  

  		  	 
	  
		  
		  

	  
	  
	  
	  	  

	  	  


	  
	  
	  

	  

	  

		

	
app = QtGui.QApplication(sys.argv)
f = MyForm()
f.show()
app.exec_()
